package com.example.module_orders.view

import android.annotation.SuppressLint
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.example.module_orders.R
import com.example.module_orders.adapter.AddOrdersImageAdapter
import com.example.module_orders.databinding.ActivityAddOrdersBinding
import com.example.module_orders.vm.OrdersViewModel
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import com.yuwei.base.BaseActivity
import com.yuwei.common.path.RouterPath
import com.yuwei.http.entity.AddOrdersBody
import dagger.hilt.android.AndroidEntryPoint
import java.io.File
import java.text.SimpleDateFormat

@AndroidEntryPoint
@Route(path = RouterPath.add_ORDERS)
class AddOrdersActivity : BaseActivity<ActivityAddOrdersBinding,OrdersViewModel>() {
    private lateinit var addOrdersImageAdapter: AddOrdersImageAdapter
    private var addOrderSImageList = mutableListOf<String>()
    @SuppressLint("SimpleDateFormat")
    override fun initView() {
        binding.addOrdersImageRv.layoutManager = GridLayoutManager(this,4)
        addOrdersImageAdapter = AddOrdersImageAdapter()
        binding.addOrdersImageRv.adapter = addOrdersImageAdapter

        addOrdersImageAdapter.data.add("+")
        addOrdersImageAdapter.notifyDataSetChanged()

        addOrdersImageAdapter.setOnItemClickListener { adapter, view, position ->
            if (position == adapter.data.size - 1 && adapter.data.size < 5) {
                PictureSelector.create(this, PictureSelector.SELECT_REQUEST_CODE).selectPicture()
            }
        }
        binding.addOrdersTj.setOnClickListener {
            val timeMillis = System.currentTimeMillis()
            val format = SimpleDateFormat("yyyy-MM-dd mm:HH:dd")
            val format1 = format.format(timeMillis)
            val addOrdersBody = AddOrdersBody(
                contactMethod = binding.addOrdersEtPhone.text.toString().toLong(),
                faultDescription = binding.addOrdersEtMs.text.toString(),
                faultPhotos = addOrderSImageList.toString(),
                id = 0,
                orderNumber = "WX${ System.currentTimeMillis() }",
                processingStatus = "待处理",
                repairLocation = 1,
                repairName=binding.addOrdersEtName.text.toString(),
                repairTime = format1,
                userId = 1
            )
            vm.getAddOrder(addOrdersBody)
        }
        binding.addOrdersBack.setOnClickListener {
            finish()
        }
    }

    override fun initData() {
        vm.addOrderSuccess.observe(this){
            ToastUtils.showLong("添加成功")
            finish()
        }
    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == PictureSelector.SELECT_REQUEST_CODE && resultCode == RESULT_OK) {
            val bundle = data?.extras
            val bean = bundle?.getParcelable<PictureBean>(PictureSelector.PICTURE_RESULT)
            val filePath = bean?.path
            val file = File(filePath)
            val sizeMB = file.length().toDouble()/(1024*1024)
            val format = "%.2f MB".format(sizeMB)
            if (filePath != null && format <= "5.00MB") {
                addOrderSImageList.add(0, filePath)
                addOrdersImageAdapter.data.add(0,filePath)
                if (addOrdersImageAdapter.data.size == 5){
                    ToastUtils.showLong("最多四张图片")
                    addOrdersImageAdapter.data.removeAt(addOrdersImageAdapter.data.size - 1)
                }
                addOrdersImageAdapter.notifyDataSetChanged()
            }else{
                ToastUtils.showLong("每张不可超过5M")
            }
        }
    }

}